using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EC2Manage.com.amazonaws.s3;

namespace EC2Manage
{
    public partial class ImageForm : Form
    {
        AWSLibrary awsLib;

        public ImageForm(AWSLibrary awsLibIn)
        {
            InitializeComponent();
            awsLib = awsLibIn;
        }

        private void ImageForm_Load(object sender, EventArgs e)
        {
            RefreshImageList();
        }

        private void RefreshImageList()
        {
            dataGridView1.Rows.Clear();
            DescribeImagesType dit = new DescribeImagesType();
            //DescribeImagesOwnerType diit = new DescribeImagesOwnerType();
            DescribeImagesOwnerType diit;

            List<DescribeImagesOwnerType> oList = new List<DescribeImagesOwnerType>();

            if (selfToolStripMenuItem.Checked)
            {
                diit = new DescribeImagesOwnerType();
                diit.owner = "self";
                oList.Add(diit);
            }
            if (amazonToolStripMenuItem.Checked)
            {
                diit = new DescribeImagesOwnerType();
                diit.owner = "amazon";
                oList.Add(diit);
            }

            if (oList.Count > 0)
            {
                dit.ownersSet = oList.ToArray();
            }


            DescribeImagesResponseType dirt = awsLib.ec2.DescribeImages(dit);

            foreach (DescribeImagesResponseItemType d in dirt.imagesSet)
            {
                string[] myList = new string[9];
                myList[0] = d.imageId;
                myList[1] = d.imageLocation;
                myList[2] = d.imageOwnerId;
                myList[3] = d.imageState;
                myList[4] = d.imageType;
                myList[5] = d.isPublic.ToString();
                myList[6] = d.kernelId;
                myList[7] = d.ramdiskId;
                myList[8] = d.architecture;
                dataGridView1.Rows.Add(myList);
            }
        }

        private void selfToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selfToolStripMenuItem.Checked)
            {
                selfToolStripMenuItem.Checked = false;
            }
            else
            {
                selfToolStripMenuItem.Checked = true;
                allToolStripMenuItem.Checked = false;
            }
            RefreshImageList();
        }

        private void amazonToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (amazonToolStripMenuItem.Checked) { amazonToolStripMenuItem.Checked = false; }
            else
            {
                amazonToolStripMenuItem.Checked = true;
                allToolStripMenuItem.Checked = false;
            }
            RefreshImageList();
        }

        private void allToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (allToolStripMenuItem.Checked)
            {
                selfToolStripMenuItem.Checked = false;
                amazonToolStripMenuItem.Checked = false;
            }
            RefreshImageList();
        }
    }
}